﻿using System;
using System.IO;
using LatoolNet;
namespace NumericalSolution {

  class ConvectionDiffusion1DMain {

    static String outfile = "c:/nsworkspace/cd1d.csv";
    static double rho = 1.0;
    static double gamma = 0.1;
    static double u = 0.1;
    static int numberOfControlColumes = 10;

    public static void Main() {

      ConvectionDiffusion1D sim = new ConvectionDiffusion1DCentral();

      sim.NumberOfControlVolumes = numberOfControlColumes;
      sim.Rho = rho;
      sim.Gamma = gamma;
      sim.U = u;

      Matrix result = sim.Solve();

      StreamWriter writer = File.CreateText(outfile);
      writer.WriteLine("x, exact, numerical");
      for (int i = 0; i < numberOfControlColumes; i++) {
        double x = sim.posx(i);
        double exact = phi(x);
        double numerical = result[i, 0];

        writer.Write(x + ", ");
        writer.Write(exact + ", ");
        writer.WriteLine(numerical);
      }
      writer.Close();
    }

    static double phi(double x) {
      double phi_0 = 1.0;
      double phi_L = 0.0;
      double L = 1.0;

      double ret = (Math.Exp(rho * u * x / gamma) - 1) 
                 / (Math.Exp(rho * u * L / gamma) - 1)
                 * (phi_L - phi_0) + phi_0;
      return ret;
                  
    }
  }
}
